﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CloudPms.Common.Utils;
using CloudPms.Entity.Entities;

namespace CloudPms.Common.CacheProvider
{
    /// <summary>
    /// 枚举助手
    /// <para>--------------------------</para>
    /// <para>@Author: cnalexi@gmail.com</para> 
    /// <para>@Create: 2012/3/30</para>  
    /// </summary>
    public class EnumHelper
    {
        public const string BASE_PARENT_KEY = "0";

        private static readonly object locker = new object();

        private static volatile IDictionary<string, string> codeParentKeyDict;

        /// <summary>
        /// 获取枚举名称字符 
        /// <para>用法：string str=EnumHelper.GetCode(status)</para>
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="value">枚举数字值</param>
        /// <returns>枚举名称字符</returns>
        public static string GetCode<T>(T value) where T : struct, IConvertible
        {
            return Enum.GetName(typeof(T), value);
        }

        /// <summary>
        /// 获取枚举数字值
        /// <para>用法：RoomStatus status=EnumHelper.GetValue(statusString)</para>
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="code">枚举名称字符</param>
        /// <returns>枚举数字值</returns>
        public static T GetValue<T>(string code) where T : struct, IConvertible
        {
            return (T)Enum.Parse(typeof(T), code, true);
        }

        /// <summary>
        /// 获取枚举字典的KeyName中文描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetName<T>(T t) where T : struct, IConvertible
        {
            string name = string.Empty;

            try
            {
                string parentKey = GetParentKey(typeof(T).Name);

                string value = GetCode<T>(t);

                //var code = BizCodeCache.GetKeyName(GlobalConst.ROOT_HOTEL_ID, parentKey, keyValue);

                //if (code != null) return code.KeyName;

                return string.Empty;

            }
            catch{}

            return name;
        }

        /// <summary>
        /// 获取枚举字典的KeyName中文描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetName<T>(string code) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }

            T t = GetValue<T>(code);

            return GetName(t);
        }
        
        public static IEnumerable<DictCode> GetCodes(string parentKey)
        {
            //return BizCodeCache.GetBizCode(GlobalConst.ROOT_HOTEL_ID, parentKey);
            return null;
        }

        public static IEnumerable<DictCode> GetCodes<T>()
        {
            IEnumerable<DictCode> codes = null;

            try
            {
                string parentKey = GetParentKey(typeof(T).Name);

                codes = new List<DictCode>();
                //codes = BizCodeCache.GetBizCode(GlobalConst.ROOT_HOTEL_ID, parentKey);

            }
            catch {}

            return codes;
        }

        public static string GetParentKey(string typeName)
        {
            var dict = GetCodeParentKeyDict();

            if (dict.ContainsKey(typeName))
            {
                return dict[typeName];
            }

            return string.Empty;
        }

        private static IDictionary<string, string> GetCodeParentKeyDict()
        {
            if (codeParentKeyDict == null)
            {
                lock (locker)
                {
                    if (codeParentKeyDict == null)
                    {
                        codeParentKeyDict = InitCodeParentKeyDict();
                    }
                }

            }
            return codeParentKeyDict;
        }

        private static IDictionary<string, string> InitCodeParentKeyDict()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();

            dict.Add(typeof (AccountType).Name, EnumCode.AccountType);

            dict.Add(typeof (BillStatus).Name, EnumCode.BillStatus);

            //商圈 无枚举则用ParentCode
            dict.Add(EnumCode.BizArea, EnumCode.BizArea);

            dict.Add(typeof (CheckInType).Name, EnumCode.CheckInType);

            dict.Add(EnumCode.Country, EnumCode.Country);

            dict.Add(typeof (GuestSourceType).Name, EnumCode.GuestSourceType);
            
            dict.Add(EnumCode.IDType, EnumCode.IDType);

            dict.Add(EnumCode.InvoiceType, EnumCode.InvoiceType);

            dict.Add(EnumCode.Inport, EnumCode.Inport);

            dict.Add(EnumCode.MemberCardStatus, EnumCode.MemberCardStatus);

            dict.Add(EnumCode.Nation, EnumCode.Nation);

            dict.Add(EnumCode.NativePlace, EnumCode.NativePlace);

            dict.Add(EnumCode.ResvChannel, EnumCode.ResvChannel);

            dict.Add(EnumCode.VisaType, EnumCode.VisaType);
            
            dict.Add(typeof (OrderStatus).Name, EnumCode.OrderStatus);

            dict.Add(typeof (RoomAttribute).Name, EnumCode.RoomAttribute);

            dict.Add(typeof (RoomStatus).Name, EnumCode.RoomStatus);

            return dict;
            
        }


    }
}
